<?php
namespace app\common\service;
use app\common\model\Questions;
use app\common\model\QuestionDb;
use app\common\model\Qbank;
use app\common\model\Paper;
use app\common\logic\DrawQuestion;
use think\Model;
use think\Db;

/* 试卷抽题相关的业务类 */
class MonitorRule extends Model
{
	
	
	/* 所有错误提示信息 */
	

	public function __construct(){
	}
	
	public function getError()
	{
		return $this->error;		
	}
	
	public function setError($str){
		$this->error = $str;
	}


    /**
     * 根据类型解析监控规则
     * @param $rule
     * @param $type
     * @param null $parm
     * @return array|bool
     */
	public function deMonitorRule($rule,$type,$parm = null){
	    if (!isset($rule) || !isset($type) )return false;
        $result = [];
	    switch ($type){
            case 1:
                $results= explode(',',$rule);
                foreach ($results as $key=>$val){
                    if ($val=='start'){
                        $result[]=0;
                    }else if ($val=='random'){
                       $PaperModel = new Paper();
                       $DrawQuestionModel = new DrawQuestion();
                       $p_duration = $PaperModel->getListByArgOne(['id'=>$parm],'ratio');
                       $ss = $DrawQuestionModel->deRation($p_duration['ratio']);
                       $questionN = 0;
                       foreach ($ss as $val) {
                           $questionN += $val[2];
                       }
                       $num = mt_rand(9,$questionN);//从第9题开始
                       $result[]=$num;
                    }else if ($val=='end'){
                        $result[]=-1;
                    }else if ($val=='must' || $val=='start'){
                        $result[]=-2;
                    }
                }
                break;
            default:
                ;
        }

        return $result;

	}		

    public function getRuleById($ids){
        if(empty($ids)) return false;
        $result = Db::name('monitor_rule')->field('rule,type')->where('id','in',$ids)->select();
        return $result;
    }

	public function getAuthStatus(){

    }
	/**
     *
     */
//	public function
	
}
	